/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUIs;

import Configs.Config;
import Controllers.BieuThucChinhQuy;
import Controllers.DateNTime;
import Controllers.NhanVienController;
import javax.swing.JOptionPane;
import models.GioiTinh;
import models.NhanVien;

/**
 *
 * @author Dinh Nhan
 */
public class f_nhanvien_them extends javax.swing.JFrame {

    private NhanVien nvcapnhat;
    private Config conn;
    private f_quanly f_qly;
    private NhanVienController nvc;
    private static f_nhanvien_them _instance;
    private BieuThucChinhQuy btcq;
    public f_nhanvien_them() {
        initComponents();
    }
    
    public NhanVien getNV(){
        return this.nvcapnhat;
    }
    public f_nhanvien_them(NhanVien nv, Config conn,f_quanly f_qly) {
        this.nvcapnhat = nv;
        this.conn = conn;
        this.f_qly = f_qly;
        this.nvc = new NhanVienController(this.conn);
        initComponents();
        lbmsnv.setText(String.valueOf( nvc.taoMSNV()));
    }
    
     public static f_nhanvien_them getInstance(NhanVien nv, Config conn,f_quanly f_qly) {
        if (_instance == null) {
            _instance = new f_nhanvien_them(nv,conn,f_qly);
        }
        return _instance;
    }

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    }                                 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        tfhoten = new javax.swing.JTextField();
        tfcmnd = new javax.swing.JTextField();
        dpngaysinh = new org.jdesktop.swingx.JXDatePicker();
        rbtnnam = new javax.swing.JRadioButton();
        rbtnnu = new javax.swing.JRadioButton();
        tfsdt = new javax.swing.JTextField();
        btnthem = new javax.swing.JButton();
        btnlammoi = new javax.swing.JButton();
        btnthoat = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        lbmsnv = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm nhân viên");
        setResizable(false);

        jLabel1.setText("MSNV :");

        jLabel2.setText("Họ Tên :");

        jLabel3.setText("Ngày Sinh :");

        jLabel4.setText("Giới Tính :");

        jLabel5.setText("Số CMND :");

        jLabel6.setText("Số Điện Thoại :");

        tfhoten.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfhotenKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfhotenKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tfhotenKeyTyped(evt);
            }
        });

        buttonGroup1.add(rbtnnam);
        rbtnnam.setSelected(true);
        rbtnnam.setText("Nam");

        buttonGroup1.add(rbtnnu);
        rbtnnu.setText("Nữ");

        btnthem.setText("Thêm");
        btnthem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthemActionPerformed(evt);
            }
        });

        btnlammoi.setText("Làm Mới");
        btnlammoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlammoiActionPerformed(evt);
            }
        });

        btnthoat.setText("Thoát");
        btnthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthoatActionPerformed(evt);
            }
        });

        jLabel9.setText("*");

        jLabel10.setText("*");

        jLabel11.setText("*");

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel12.setText("THÊM THÔNG TIN NHÂN VIÊN");

        lbmsnv.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(112, 112, 112)
                        .addComponent(jLabel7))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(73, 73, 73)
                                .addComponent(btnthem, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnlammoi)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                                .addComponent(btnthoat))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(49, 49, 49)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(dpngaysinh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(tfhoten, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(tfcmnd, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addComponent(rbtnnam)
                                                .addGap(18, 18, 18)
                                                .addComponent(rbtnnu))
                                            .addComponent(tfsdt, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                                            .addComponent(lbmsnv, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel12)
                .addGap(37, 37, 37))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lbmsnv, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfhoten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tfcmnd, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel10)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(dpngaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rbtnnam)
                    .addComponent(rbtnnu))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfsdt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel11))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnthem)
                    .addComponent(btnlammoi)
                    .addComponent(btnthoat))
                .addGap(70, 70, 70)
                .addComponent(jLabel7))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnlammoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlammoiActionPerformed
        tfcmnd.setText("");
        tfhoten.setText("");
        tfsdt.setText("");
    }//GEN-LAST:event_btnlammoiActionPerformed

    private void btnthemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthemActionPerformed

        this.btcq = new BieuThucChinhQuy(tfhoten.getText(), "[^!@#$%^&*\\{\\}\\\\'\\\"]+$");
        GioiTinh gioitinh;
        if(rbtnnam.isSelected()){
            gioitinh = GioiTinh.Nam;
        }
        else gioitinh = GioiTinh.Nữ;
        boolean Check2 = this.nvc.getWithCMND(tfcmnd.getText());
        if(tfcmnd.getText().equals("") || tfhoten.getText().equals("") || tfsdt.getText().equals("")){
            JOptionPane.showMessageDialog(this, "Vui lòng kiểm tra thông tin");
        }
        
        else if (!this.btcq.isBoolean()) {
            JOptionPane.showMessageDialog(this, "Họ tên không hợp lệ.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            this.tfhoten.requestFocus();
        }
        else if (!DateNTime.checkTuoi(dpngaysinh.getDate())) {
            JOptionPane.showMessageDialog(this, "Ngày tháng năm sinh không hợp lệ.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            this.dpngaysinh.requestFocus();
        }
        else{
            this.btcq.setBtchinhquy("^[0-9]{9,15}$");
            this.btcq.setChuoikt(this.tfsdt.getText());
            if (!this.btcq.isBoolean()) {
                JOptionPane.showMessageDialog(this, "Số điện thoại không hợp lệ.", "Thông báo", JOptionPane.ERROR_MESSAGE);
                this.tfsdt.requestFocus();
            }
            else if(Check2){
            NhanVien nv = new NhanVien(tfcmnd.getText(), tfhoten.getText(), gioitinh, tfsdt.getText(), dpngaysinh.getDate());
            nv.setMSNV(lbmsnv.getText());
            this.nvc.insertNhanVien(nv);
            int ktra = JOptionPane.showConfirmDialog(this, "them tai khoản thành công,bạn có muốn tiếp tục thêm", "Warning", JOptionPane.YES_NO_OPTION);
            this.f_qly.getMyFormat().clearTable(this.f_qly.getTbnv());
            this.nvc.showAllNhanVien(f_qly.getTbnv());
            if(ktra==JOptionPane.NO_OPTION)
                this.dispose();
            }
        else{
            JOptionPane.showMessageDialog(this, "Nhân Viên Đã Tồn Tại!");
        }
        }
        
            
    }//GEN-LAST:event_btnthemActionPerformed

    private void tfhotenKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfhotenKeyTyped
       
    }//GEN-LAST:event_tfhotenKeyTyped

    private void tfhotenKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfhotenKeyPressed
        btnthem.setEnabled(true);
    }//GEN-LAST:event_tfhotenKeyPressed

    private void tfhotenKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfhotenKeyReleased
        //btnsua.setVisible(true);
    }//GEN-LAST:event_tfhotenKeyReleased

    private void btnthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnthoatActionPerformed

    /**
     * @param args the command line arguments
     */
  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnlammoi;
    private javax.swing.JButton btnthem;
    private javax.swing.JButton btnthoat;
    private javax.swing.ButtonGroup buttonGroup1;
    public org.jdesktop.swingx.JXDatePicker dpngaysinh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lbmsnv;
    private javax.swing.JRadioButton rbtnnam;
    private javax.swing.JRadioButton rbtnnu;
    private javax.swing.JTextField tfcmnd;
    private javax.swing.JTextField tfhoten;
    private javax.swing.JTextField tfsdt;
    // End of variables declaration//GEN-END:variables
}
